home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / XML Utilities / Professional Programmer XSL IDE / Xselerator25.msi / Data.Cab / F30740_strSplittoLines.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2002-01-20  |  3.7 KB  |  102 lines

  1. <xsl:stylesheet version="1.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  4. xmlns:str-split2lines-func="f:str-split2lines-func"
  5. exclude-result-prefixes="xsl msxsl str-split2lines-func"
  6. >
  7.  
  8.    <xsl:import href="str-foldl.xsl"/>
  9.  
  10.    <!-- to be applied on text.xml -->
  11.  
  12.    <str-split2lines-func:str-split2lines-func/>
  13.  
  14.    <xsl:output indent="yes" omit-xml-declaration="yes"/>
  15.    
  16.     <xsl:template match="/">
  17.       <xsl:call-template name="str-split-to-lines">
  18.         <xsl:with-param name="pStr" select="/*"/>
  19.         <xsl:with-param name="pLineLength" select="64"/>
  20.         <xsl:with-param name="pDelimiters" select="'  '"/>
  21.       </xsl:call-template>
  22.     </xsl:template>
  23.  
  24.     <xsl:template name="str-split-to-lines">
  25.       <xsl:param name="pStr"/>
  26.       <xsl:param name="pLineLength" select="60"/>
  27.       <xsl:param name="pDelimiters" select="'  '"/>
  28.       
  29.       <xsl:variable name="vsplit2linesFun"
  30.                     select="document('')/*/str-split2lines-func:*[1]"/>
  31.                     
  32.       <xsl:variable name="vrtfParams">
  33.        <delimiters><xsl:value-of select="$pDelimiters"/></delimiters>
  34.        <lineLength><xsl:copy-of select="$pLineLength"/></lineLength>
  35.       </xsl:variable>
  36.  
  37.       <xsl:variable name="vResult">
  38.           <xsl:call-template name="str-foldl">
  39.             <xsl:with-param name="pFunc" select="$vsplit2linesFun"/>
  40.             <xsl:with-param name="pStr" select="$pStr"/>
  41.             <xsl:with-param name="pA0" select="msxsl:node-set($vrtfParams)"/>
  42.           </xsl:call-template>
  43.       </xsl:variable>
  44.       
  45.       <xsl:for-each select="msxsl:node-set($vResult)/line">
  46.         <xsl:for-each select="word">
  47.           <xsl:value-of select="concat(., ' ')"/>
  48.         </xsl:for-each>
  49.         <xsl:value-of select="' '"/>
  50.       </xsl:for-each>
  51.     </xsl:template>
  52.  
  53.     <xsl:template match="str-split2lines-func:*">
  54.       <xsl:param name="arg1" select="/.."/>
  55.       <xsl:param name="arg2"/>
  56.          
  57.       <xsl:copy-of select="msxsl:node-set($arg1)/*[position() < 3]"/>
  58.       <xsl:copy-of select="msxsl:node-set($arg1)/line[position() != last()]"/>
  59.       
  60.       <xsl:choose>
  61.         <xsl:when test="contains($arg1/*[1], $arg2)">
  62.           <xsl:if test="string($arg1/word)">
  63.              <xsl:call-template name="fillLine">
  64.                <xsl:with-param name="pLine" select="$arg1/line[last()]"/>
  65.                <xsl:with-param name="pWord" select="$arg1/word"/>
  66.                <xsl:with-param name="pLineLength" select="$arg1/*[2]"/>
  67.              </xsl:call-template>
  68.           </xsl:if>
  69.         </xsl:when>
  70.         <xsl:otherwise>
  71.           <xsl:copy-of select="$arg1/line[last()]"/>
  72.           <word><xsl:value-of select="concat($arg1/word, $arg2)"/></word>
  73.         </xsl:otherwise>
  74.       </xsl:choose>
  75.     </xsl:template>
  76.       
  77.       <!-- Test if the new word fits into the last line -->
  78.     <xsl:template name="fillLine">
  79.       <xsl:param name="pLine" select="/.."/>
  80.       <xsl:param name="pWord" select="/.."/>
  81.       <xsl:param name="pLineLength" />
  82.       
  83.       <xsl:variable name="vnWordsInLine" select="count($pLine/word)"/>
  84.       <xsl:variable name="vLineLength" select="string-length($pLine) + $vnWordsInLine"/>
  85.       <xsl:choose>
  86.         <xsl:when test="not($vLineLength + string-length($pWord) > $pLineLength)">
  87.           <line>
  88.             <xsl:copy-of select="$pLine/*"/>
  89.             <xsl:copy-of select="$pWord"/>
  90.           </line>
  91.         </xsl:when>
  92.         <xsl:otherwise>
  93.           <xsl:copy-of select="$pLine"/>
  94.           <line>
  95.             <xsl:copy-of select="$pWord"/>
  96.           </line>
  97.           <word/>
  98.         </xsl:otherwise>
  99.       </xsl:choose>
  100.     </xsl:template>
  101.  
  102. </xsl:stylesheet>